/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import User.MessageDTO;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class Login extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {
            boolean flag = false;
            // 使用字符缓冲输入流读取user.txt文件中的内容
            BufferedReader br = null;
            br = new BufferedReader(new FileReader("F:\\javaEE\\IdeaProjects\\exp3\\src\\main\\user.txt"));
            // 一次读取一行
            String line = null;
            PrintWriter pw=response.getWriter();
            while ((line = br.readLine()) != null) {
                // 字符串的分割功能
                String[] datas = line.split(" ");
                // 判断
                if (datas[0].equals(request.getParameter("username")))
                {   if(datas[2].equals(request.getParameter("password")))
                {
                    MessageDTO message=new MessageDTO(datas[0],datas[1],datas[2],datas[3],datas[4]);
                    request.getSession().setAttribute("LocalUser",message);
                    flag = true;
                    break;
                }
                else
                {
                    pw.println("<center><button onclick=\"window.location.href='./login.jsp'\">重新登录</button></center>");
                    pw.println("<script language='javascript'>alert('密码错误,请重新输入')</script>");
                    break;
                }
                }
            }
            if(flag==true)
            {

                response.sendRedirect("/show.jsp");

            }
            if(line==null)
            {
                pw.println("<center><button onclick=\"window.location.href='./login.jsp'\">重新登录</button></center>");
                pw.println("<center><button onclick=\"window.location.href='./register.jsp'\">前往注册</button></center>");
                pw.println("<script language='javascript'>alert('用户不存在,请重新登录或者前往注册')</script>");
            }
        }
        finally {

        }



    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

